import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Leet_395 {
    public static void main(String[] args) {
    Leet_395 l = new Leet_395();
        System.out.println(l.longestSubstring("aaabb",3));
    }
    public int longestSubstring(String s, int k) {
        int max = 0;
        for (int i = 0; i < s.length()-k; i++) {
            for (int j = i+1; j<s.length()+1;j++){
                    String a = s.substring(i,j);
                    Set<Character> set1 = new HashSet<>();
                    for(char b:a.toCharArray()){
                        set1.add(b);
                    }
                    int[] length = new int[set1.size()];
                    int lengnum = 0;
                    for (char t:set1){
                        for (char tt:a.toCharArray()){
                            if (tt==t)
                                length[lengnum]++;
                        }
                        lengnum++;
                    }
                    boolean flag = true;
                    for (int num:length){
                        if (num < k) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag){
                        max = Math.max(max,a.length());
                    }
            }
        }
        if (k==1&&s.length()==1){
            return 1;
        }
    return max;
    }
}
